﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class DUXuatNhapTonCTBO : Csla.BusinessBase<DUXuatNhapTonCTBO>
    {
        #region Business Properties

        private int? _DUXuatNhapTonCTID = null;
        public int? DUXuatNhapTonCTID
        {
            get { return this._DUXuatNhapTonCTID; }
            set { this._DUXuatNhapTonCTID = value; PropertyHasChanged("DUXuatNhapTonCTID"); }
        }

        private int? _XuatNhapTonID = null;
        public int? XuatNhapTonID
        {
            get { return this._XuatNhapTonID; }
            set { this._XuatNhapTonID = value; PropertyHasChanged("XuatNhapTonID"); }
        }

        private int? _ThuocID = null;
        public int? ThuocID
        {
            get { return this._ThuocID; }
            set { this._ThuocID = value; PropertyHasChanged("ThuocID"); }
        }

        private string _MaThuoc = null;
        public string MaThuoc
        {
            get { return this._MaThuoc; }
            set { this._MaThuoc = value; PropertyHasChanged("MaThuoc"); }
        }

        private string _TenThuoc = null;
        public string TenThuoc
        {
            get { return this._TenThuoc; }
            set { this._TenThuoc = value; PropertyHasChanged("TenThuoc"); }
        }

        private int? _DonViTinhID = null;
        public int? DonViTinhID
        {
            get { return this._DonViTinhID; }
            set { this._DonViTinhID = value; PropertyHasChanged("DonViTinhID"); }
        }

        private string _TenDonViTinh = null;
        public string TenDonViTinh
        {
            get { return this._TenDonViTinh; }
            set { this._TenDonViTinh = value; PropertyHasChanged("TenDonViTinh"); }
        }

        private double? _DauKyTon = null;
        public double? DauKyTon
        {
            get { return this._DauKyTon; }
            set { this._DauKyTon = value; PropertyHasChanged("DauKyTon"); }
        }

        private double? _DauKyGia = null;
        public double? DauKyGia
        {
            get { return this._DauKyGia; }
            set { this._DauKyGia = value; PropertyHasChanged("DauKyGia"); }
        }

        private double? _DauKyTien = null;
        public double? DauKyTien
        {
            get { return this._DauKyTien; }
            set { this._DauKyTien = value; PropertyHasChanged("DauKyTien"); }
        }

        private double? _TrongKyNhap = null;
        public double? TrongKyNhap
        {
            get { return this._TrongKyNhap; }
            set { this._TrongKyNhap = value; PropertyHasChanged("TrongKyNhap"); }
        }

        private double? _TrongKyTienNhap = null;
        public double? TrongKyTienNhap
        {
            get { return this._TrongKyTienNhap; }
            set { this._TrongKyTienNhap = value; PropertyHasChanged("TrongKyTienNhap"); }
        }

        private double? _TrongKyXuat = null;
        public double? TrongKyXuat
        {
            get { return this._TrongKyXuat; }
            set { this._TrongKyXuat = value; PropertyHasChanged("TrongKyXuat"); }
        }

        private double? _TrongKyTienXuat = null;
        public double? TrongKyTienXuat
        {
            get { return this._TrongKyTienXuat; }
            set { this._TrongKyTienXuat = value; PropertyHasChanged("TrongKyTienXuat"); }
        }

        private double? _CuoiKyTon = null;
        public double? CuoiKyTon
        {
            get { return this._CuoiKyTon; }
            set { this._CuoiKyTon = value; PropertyHasChanged("CuoiKyTon"); }
        }

        private double? _CuoiKyGia = null;
        public double? CuoiKyGia
        {
            get { return this._CuoiKyGia; }
            set { this._CuoiKyGia = value; PropertyHasChanged("CuoiKyGia"); }
        }

        private double? _CuoiKyTien = null;
        public double? CuoiKyTien
        {
            get { return this._CuoiKyTien; }
            set { this._CuoiKyTien = value; PropertyHasChanged("CuoiKyTien"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private int? _UserCreated = Session.LoginID;
        public int? UserCreated
        {
            get { return this._UserCreated; }
            set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
        }

        private DateTime? _DateCreated = DateTime.MaxValue;
        public DateTime? DateCreated
        {
            get { return this._DateCreated; }
            set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
        }

        private int? _UserModify = null;
        public int? UserModify
        {
            get { return this._UserModify; }
            set { this._UserModify = value; PropertyHasChanged("UserModify"); }
        }

        private DateTime? _DateModify = null;
        public DateTime? DateModify
        {
            get { return this._DateModify; }
            set { this._DateModify = value; PropertyHasChanged("DateModify"); }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; PropertyHasChanged("Deleted"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _DUXuatNhapTonCTID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newDUXuatNhapTonCTID = -1;
        private DUXuatNhapTonCTBO()
        {
            _DUXuatNhapTonCTID = newDUXuatNhapTonCTID--;
        }

        public DUXuatNhapTonCTBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public DUXuatNhapTonCTBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static DUXuatNhapTonCTBO NewRootDUXuatNhapTonCTBO()
        {
            return DataPortal.Create<DUXuatNhapTonCTBO>(new RootCriteria());
        }

        internal static DUXuatNhapTonCTBO NewChildDUXuatNhapTonCTBO()
        {
            return DataPortal.Create<DUXuatNhapTonCTBO>(new ChildCriteria());
        }

        public static DUXuatNhapTonCTBO GetRootDUXuatNhapTonCTBO(int _DUXuatNhapTonCTID)
        {
            return DataPortal.Fetch<DUXuatNhapTonCTBO>(new RootCriteria(_DUXuatNhapTonCTID));
        }

        public static DUXuatNhapTonCTBO GetRootDUXuatNhapTonCTBOByWhere(string where)
        {
            return DataPortal.Fetch<DUXuatNhapTonCTBO>(where);
        }

        internal static DUXuatNhapTonCTBO GetChildDUXuatNhapTonCTBO(SqlDataReader dataReader)
        {
            return new DUXuatNhapTonCTBO(dataReader);
        }

        public static void DeleteDUXuatNhapTonCTBO(int _DUXuatNhapTonCTID)
        {
            DataPortal.Delete(new RootCriteria(_DUXuatNhapTonCTID));
        }

        public static void DeleteDUXuatNhapTonCTBO(int _DUXuatNhapTonCTID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_DUXuatNhapTonCTID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int DUXuatNhapTonCTID;
            public bool directDelete = false;
            public RootCriteria(int _DUXuatNhapTonCTID)
            {
                this.DUXuatNhapTonCTID = _DUXuatNhapTonCTID;
                this.directDelete = false;
            }

            public RootCriteria(int _DUXuatNhapTonCTID, bool directDelete)
            {
                this.DUXuatNhapTonCTID = _DUXuatNhapTonCTID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int DUXuatNhapTonCTID;
            public bool directDelete = false;
            public ChildCriteria(int _DUXuatNhapTonCTID)
            {
                this.DUXuatNhapTonCTID = _DUXuatNhapTonCTID;
                this.directDelete = false;
            }

            public ChildCriteria(int _DUXuatNhapTonCTID, bool directDelete)
            {
                this.DUXuatNhapTonCTID = _DUXuatNhapTonCTID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUXuatNhapTonCTBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[DUXuatNhapTonCTID] = " + criteria.DUXuatNhapTonCTID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUXuatNhapTonCTBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["DUXuatNhapTonCTID"] is DBNull))
                this._DUXuatNhapTonCTID = (int)dataReader["DUXuatNhapTonCTID"];
            if (!(dataReader["XuatNhapTonID"] is DBNull))
                this._XuatNhapTonID = (int)dataReader["XuatNhapTonID"];
            if (!(dataReader["ThuocID"] is DBNull))
                this._ThuocID = (int)dataReader["ThuocID"];
            if (!(dataReader["MaThuoc"] is DBNull))
                this._MaThuoc = (string)dataReader["MaThuoc"];
            if (!(dataReader["TenThuoc"] is DBNull))
                this._TenThuoc = (string)dataReader["TenThuoc"];
            if (!(dataReader["DonViTinhID"] is DBNull))
                this._DonViTinhID = (int)dataReader["DonViTinhID"];
            if (!(dataReader["TenDonViTinh"] is DBNull))
                this._TenDonViTinh = (string)dataReader["TenDonViTinh"];
            if (!(dataReader["DauKyTon"] is DBNull))
                this._DauKyTon = (double)dataReader["DauKyTon"];
            if (!(dataReader["DauKyGia"] is DBNull))
                this._DauKyGia = (double)dataReader["DauKyGia"];
            if (!(dataReader["DauKyTien"] is DBNull))
                this._DauKyTien = (double)dataReader["DauKyTien"];
            if (!(dataReader["TrongKyNhap"] is DBNull))
                this._TrongKyNhap = (double)dataReader["TrongKyNhap"];
            if (!(dataReader["TrongKyTienNhap"] is DBNull))
                this._TrongKyTienNhap = (double)dataReader["TrongKyTienNhap"];
            if (!(dataReader["TrongKyXuat"] is DBNull))
                this._TrongKyXuat = (double)dataReader["TrongKyXuat"];
            if (!(dataReader["TrongKyTienXuat"] is DBNull))
                this._TrongKyTienXuat = (double)dataReader["TrongKyTienXuat"];
            if (!(dataReader["CuoiKyTon"] is DBNull))
                this._CuoiKyTon = (double)dataReader["CuoiKyTon"];
            if (!(dataReader["CuoiKyGia"] is DBNull))
                this._CuoiKyGia = (double)dataReader["CuoiKyGia"];
            if (!(dataReader["CuoiKyTien"] is DBNull))
                this._CuoiKyTien = (double)dataReader["CuoiKyTien"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["UserCreated"] is DBNull))
                this._UserCreated = (int)dataReader["UserCreated"];
            if (!(dataReader["DateCreated"] is DBNull))
                this._DateCreated = (DateTime)dataReader["DateCreated"];
            if (!(dataReader["UserModify"] is DBNull))
                this._UserModify = (int)dataReader["UserModify"];
            if (!(dataReader["DateModify"] is DBNull))
                this._DateModify = (DateTime)dataReader["DateModify"];
            if (!(dataReader["Deleted"] is DBNull))
                this._Deleted = (bool)dataReader["Deleted"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._UserCreated = Session.LoginID;
            this._DateCreated = DateTime.MaxValue;
            this._UserModify = null;
            this._DateModify = null;
            this._Deleted = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUXuatNhapTonCTBO_InsertBO");
            db.AddInParameter(cmd, "@XuatNhapTonID", SqlDbType.Int, this._XuatNhapTonID);
            db.AddInParameter(cmd, "@ThuocID", SqlDbType.Int, this._ThuocID);
            db.AddInParameter(cmd, "@MaThuoc", SqlDbType.NVarChar, this._MaThuoc);
            db.AddInParameter(cmd, "@TenThuoc", SqlDbType.NVarChar, this._TenThuoc);
            db.AddInParameter(cmd, "@DonViTinhID", SqlDbType.Int, this._DonViTinhID);
            db.AddInParameter(cmd, "@TenDonViTinh", SqlDbType.NVarChar, this._TenDonViTinh);
            db.AddInParameter(cmd, "@DauKyTon", SqlDbType.Float, this._DauKyTon);
            db.AddInParameter(cmd, "@DauKyGia", SqlDbType.Float, this._DauKyGia);
            db.AddInParameter(cmd, "@DauKyTien", SqlDbType.Float, this._DauKyTien);
            db.AddInParameter(cmd, "@TrongKyNhap", SqlDbType.Float, this._TrongKyNhap);
            db.AddInParameter(cmd, "@TrongKyTienNhap", SqlDbType.Float, this._TrongKyTienNhap);
            db.AddInParameter(cmd, "@TrongKyXuat", SqlDbType.Float, this._TrongKyXuat);
            db.AddInParameter(cmd, "@TrongKyTienXuat", SqlDbType.Float, this._TrongKyTienXuat);
            db.AddInParameter(cmd, "@CuoiKyTon", SqlDbType.Float, this._CuoiKyTon);
            db.AddInParameter(cmd, "@CuoiKyGia", SqlDbType.Float, this._CuoiKyGia);
            db.AddInParameter(cmd, "@CuoiKyTien", SqlDbType.Float, this._CuoiKyTien);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
            db.AddOutParameter(cmd, "@NewDUXuatNhapTonCTID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._DUXuatNhapTonCTID = (int)cmd.Parameters["@NewDUXuatNhapTonCTID"].Value;
            this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._UserModify = Session.LoginID;
            this._DateModify = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUXuatNhapTonCTBO_UpdateBO");
            db.AddInParameter(cmd, "@DUXuatNhapTonCTID", SqlDbType.Int, this._DUXuatNhapTonCTID);
            db.AddInParameter(cmd, "@XuatNhapTonID", SqlDbType.Int, this._XuatNhapTonID);
            db.AddInParameter(cmd, "@ThuocID", SqlDbType.Int, this._ThuocID);
            db.AddInParameter(cmd, "@MaThuoc", SqlDbType.NVarChar, this._MaThuoc);
            db.AddInParameter(cmd, "@TenThuoc", SqlDbType.NVarChar, this._TenThuoc);
            db.AddInParameter(cmd, "@DonViTinhID", SqlDbType.Int, this._DonViTinhID);
            db.AddInParameter(cmd, "@TenDonViTinh", SqlDbType.NVarChar, this._TenDonViTinh);
            db.AddInParameter(cmd, "@DauKyTon", SqlDbType.Float, this._DauKyTon);
            db.AddInParameter(cmd, "@DauKyGia", SqlDbType.Float, this._DauKyGia);
            db.AddInParameter(cmd, "@DauKyTien", SqlDbType.Float, this._DauKyTien);
            db.AddInParameter(cmd, "@TrongKyNhap", SqlDbType.Float, this._TrongKyNhap);
            db.AddInParameter(cmd, "@TrongKyTienNhap", SqlDbType.Float, this._TrongKyTienNhap);
            db.AddInParameter(cmd, "@TrongKyXuat", SqlDbType.Float, this._TrongKyXuat);
            db.AddInParameter(cmd, "@TrongKyTienXuat", SqlDbType.Float, this._TrongKyTienXuat);
            db.AddInParameter(cmd, "@CuoiKyTon", SqlDbType.Float, this._CuoiKyTon);
            db.AddInParameter(cmd, "@CuoiKyGia", SqlDbType.Float, this._CuoiKyGia);
            db.AddInParameter(cmd, "@CuoiKyTien", SqlDbType.Float, this._CuoiKyTien);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
            db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

            db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_DUXuatNhapTonCTID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUXuatNhapTonCTBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@DUXuatNhapTonCTID", SqlDbType.Int, criteria.DUXuatNhapTonCTID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_DUXuatNhapTonCTID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
